﻿/* $RCSFile: CrmProducts.asmx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ECommerce/Services/CrmProducts.asmx.cs $
 * $log$
 * Revision 21 2010/08/10 17:55:42  christian.surieux
 *   Missing comments in asax asmx ashx files
 * Revision 8 2010/08/09 10:07:05  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using AddonNice.CRMWrapper;
using AddonNice.CRMWrapper.Design;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;
using Cache2    =   AddonNice.Settings.Cache.CurrentCache;

namespace AddonNice.ECommerce.Services
{
    /// <summary>
    /// Summary description for CrmProducts
    /// </summary>
    [WebService(Namespace = "http://addonnice.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class CrmProducts : System.Web.Services.WebService
    {

        protected CRMWrapperBase Wrapper
        {
            get
            {
                return AddonNice.Security.PortalSecurity.Wrapper;
            }
        }

        [WebMethod]
        public RadComboBoxItemData[] GetProducts(object context)
        {
            // We cannot use a dictionary as a parameter, because it is only supported by script services.
            // The context object should be cast to a dictionary at runtime.
            IDictionary<string, object> contextDictionary   =   (IDictionary<string, object>)context;
		    string filterString                             =   ((string)contextDictionary["FilterString"]).ToLower()+"%";
            List<CrmListItem> lst                           =   Wrapper.Util.EntitiesLike(CrmStr.product,CrmStr.productid,CrmStr.name,filterString,100,true);
            RadComboBoxItemData[] result                    =   new RadComboBoxItemData[lst.Count];
		    for (int i=0 ; i < lst.Count ; i++)
		    {
                CrmListItem it                      =   lst[i];
			    RadComboBoxItemData itemData        =   new RadComboBoxItemData();
                itemData.Text                       =   it.name;
                itemData.Value                      =   it.ID.ToString();
			    result[i]                           =   itemData;
		    }
		    return result;
        }
    }
}
